using System;
using System.Text;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using FAQComponents;
using FAQComponents.Objects;
using FAQComponents.API;
using Adge.UI.WebControls;


namespace Adge.Web.Faq.Admin
{
	/// <summary>
	/// Summary description for UserAdmin.
	/// </summary>
	public partial class UserAdmin : BidirectionalSortableDataGridPage
	{
	
		protected void Page_Load(object sender, System.EventArgs e)
		{
			PermissionAPI.EnsureAdministrator();

			if (!Page.IsPostBack)
				BindData();
		}

		private void BindData()
		{
			UserCollection userList = UserAPI.GetAllUsers();

			base.BindData(dgUsers, userList);
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.dgUsers.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.dgUsers_PageIndexChanged);
			this.dgUsers.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgUsers_CancelCommand);
			this.dgUsers.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgUsers_EditCommand);
			this.dgUsers.SortCommand += new System.Web.UI.WebControls.DataGridSortCommandEventHandler(this.dgUsers_SortCommand);
			this.dgUsers.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgUsers_UpdateCommand);
			this.dgUsers.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgUsers_DeleteCommand);
			this.dgUsers.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.dgUsers_ItemDataBound);

		}
		#endregion

		#region Add User-Related Event Handlers
		protected void lnkCreateUser_Click(object sender, System.EventArgs e)
		{
			// set the display of the DataGrid and create user UI
			dgUsers.Visible = false;
			this.pnlCreateUserUI.Visible = true;
		}

		protected void btnCancelCreate_Click(object sender, System.EventArgs e)
		{
			// set the display of the DataGrid and create user UI
			dgUsers.Visible = true;
			this.pnlCreateUserUI.Visible = false;

			DGHelpers.ResetDataGrid(dgUsers);
			BindData();
		}

		protected void btnAdd_Click(object sender, System.EventArgs e)
		{
			// Create a user, if the page is valid
			if (!Page.IsValid) return;

			User u = new User();
			u.Username = usernameAdd.Text;
			u.Password = passwordAdd.Text;
			u.PublicEmail = publicEmailAdd.Text;
			u.PrivateEmail = privateEmailAdd.Text;
			u.IsAdmin = isAdministrator.Checked;
			u.Active = isActive.Checked;

			UserAPI.AddUser(u);

			// set the display of the DataGrid and create user UI
			dgUsers.Visible = true;
			this.pnlCreateUserUI.Visible = false;

			DGHelpers.ResetDataGrid(dgUsers);
			BindData();
		}

		private void ClearInputFormFields()
		{
			usernameAdd.Text = passwordAdd.Text = publicEmailAdd.Text = privateEmailAdd.Text = string.Empty;
			isAdministrator.Checked = false;
			isActive.Checked = true;
		}
		#endregion

		#region DataGrid Event Handlers
		#region SortCommand
		private void dgUsers_SortCommand(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
		{
            base.HandleSorting(dgUsers, e.SortExpression);
			BindData();
		}
		#endregion

		#region DeleteCommand
		private void dgUsers_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
		{
			UserAPI.DeleteUser((int) dgUsers.DataKeys[e.Item.ItemIndex]);

			if (dgUsers.Items.Count == 1)		// we deleted the last item on this page
				dgUsers.CurrentPageIndex = Math.Max(0, dgUsers.CurrentPageIndex - 1);

			BindData();
		}
		#endregion

		#region Editing-Related Event Handlers
		private void dgUsers_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
		{
			dgUsers.EditItemIndex = -1;
			BindData();
		}

		private void dgUsers_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
		{
			dgUsers.EditItemIndex = e.Item.ItemIndex;
			BindData();
		}

		private void dgUsers_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
		{
			// grab the values and update the user
			User u = new User();

			u.UserID = (int) dgUsers.DataKeys[e.Item.ItemIndex];
			u.Username = ((TextBox) e.Item.FindControl("usernameEdit")).Text;
			u.Password = ((TextBox) e.Item.FindControl("passwordEdit")).Text;
			u.PublicEmail = ((TextBox) e.Item.FindControl("publicEmailEdit")).Text;
			u.PrivateEmail = ((TextBox) e.Item.FindControl("privateEmailEdit")).Text;
			u.IsAdmin = ((CheckBox) e.Item.FindControl("isAdminEdit")).Checked;
			u.Active = ((CheckBox) e.Item.FindControl("activeEdit")).Checked;

			UserAPI.UpdateUser(u);
			
			dgUsers.EditItemIndex = -1;
			BindData();
		}
		#endregion

		#region PageIndexChanged
		private void dgUsers_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
		{
			base.HandlePaging(dgUsers, e.NewPageIndex);
			BindData();
		}
		#endregion

		#region ItemDataBound
		private void dgUsers_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
		{
			if (e.Item.ItemType != ListItemType.Header && e.Item.ItemType != ListItemType.Footer)
			{
				// add a client-side confirm to the Delete button
				LinkButton db = (LinkButton) e.Item.Cells[1].Controls[0];
				db.Attributes["onclick"] = "javascript:if (!confirm('This will permanently delete this user, all of his answers to others\\' FAQs, and all of his FAQs, including answers others have contributed.  Are you sre you want to do this?')) return false; else return confirm('Are you really sure?  I mean, 100%, absolutely, positively sure?');";
			}
		}
		#endregion
		#endregion

	}
}
